package com.sskj.gocoin.v.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.rxbus2.RxBus;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.v.activity.CommonActivity;
import com.sskj.gocoin.v.activity.LoginActivity;
import com.sskj.gocoin.v.activity.MainActivity;
import com.sskj.gocoin.v.activity.SecurityFailedActivity;
import com.sskj.gocoin.v.activity.VerifyJuniorActivity;
import com.sskj.gocoin.v.widget.mychart.ProgressDialogs;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

import static com.sskj.gocoin.common.Constants.VERIFY_IDENT_CODE;

/**
 * Created by Lee on 2018/1/25 0025.
 */

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements IBaseView {
    public T mPresenter;
    public BaseActivity mActivity;


    private ProgressDialogs mProgressDialogs;
    private int mRequestCount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mProgressDialogs = new ProgressDialogs(this);
        setContentView(getLayoutResId());
        mActivity = this;
        mPresenter = getPresenter();
        mPresenter.attachView(this, this);
//        mMobile = (String) SPUtils.get(this, Constants.SP_MOBILE, "");
//        Constants.mIsLogin = (boolean) SPUtils.get(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, false);
        onViewBinding();
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.cancelRequest();
        mPresenter.detachView();
    }

    protected void initTitle(String title) {
        View ivBack = findViewById(R.id.iv_back);
        if (ivBack != null) {
            ClickUtil.click(ivBack, this::finish);
        }
        TextView titleView = findViewById(R.id.iv_title);
        if (titleView != null) {
            titleView.setText(title);
        }

    }

    protected void initData() {
    }

    protected void initView() {
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        ButterKnife.bind(this);
    }

    /**
     * 初始化布局ID
     *
     * @return
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化当前View 的 presenter
     *
     * @return
     */
    public abstract T getPresenter();

    /**
     * 初始化
     */
    protected void onViewBinding() {
    }

    ;

    public void showLoading() {
        if (mProgressDialogs != null && mRequestCount <= 0) {
            mRequestCount = 1;
            mProgressDialogs.showDialog();
        } else {
            ++mRequestCount;
        }
    }

    public void hideLoading() {
        if (mRequestCount <= 1) {
            mRequestCount = 0;
            mProgressDialogs.closeDialog();
        } else {
            --mRequestCount;
        }
    }

    public void setText(TextView view, String text) {
        if (view != null && text != null&&!text.isEmpty()) {
            view.setText(text);
        }
    }

    /**
     * 判断用户初级实名认证
     */
    protected boolean checkIdent() {

        if (MyAppLication.getRealnameStatus() == 3) {//已实名认证
            return true;
        } else if (MyAppLication.getRealnameStatus() == 2) {//审核中
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity201), false);
            return false;
        } else if (MyAppLication.getRealnameStatus() == 4) {//被拒绝
//            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity202), true);
            SecurityFailedActivity.start(this, App.INSTANCE.getString(R.string.strBaseActivity203));
            return false;
        } else {
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity204), true);
            return false;
        }
    }

    /**
     * 判断用户高级实名认证
     */
    protected boolean checkIdentHign(Context context) {

        if (MyAppLication.getRealnameStatusHigh() == 3) {//已实名认证
            return true;
        } else if (MyAppLication.getRealnameStatusHigh() == 2) {//审核中
            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity205), false);
            return false;
        } else if (MyAppLication.getRealnameStatusHigh() == 4) {//被拒绝
//            showApplyTip(App.INSTANCE.getString(R.string.strBaseActivity202), true);
            SecurityFailedActivity.start(this, App.INSTANCE.getString(R.string.strBaseActivity207));
            return false;
        } else {
            startActivity(new Intent(context, CommonActivity.class));
            return false;
        }
    }

    /**
     * 判断用户实名认证弹窗
     */
    private void showApplyTip(String msg, boolean status) {
        AlertDialog tipDialog = new AlertDialog.Builder(this)
                .setTitle(App.INSTANCE.getString(R.string.strBaseActivity208))
                .setMessage(msg)
                .setPositiveButton(status ? App.INSTANCE.getString(R.string.strBaseActivity209) : App.INSTANCE.getString(R.string.strBaseActivity210), (dialog, which) -> {
                    dialog.dismiss();
                    if (status) {
//                        openActivity(VerifyIdentActivity.class, VERIFY_IDENT_CODE);
                        openActivity(VerifyJuniorActivity.class, VERIFY_IDENT_CODE);  // 初级认证
                    }
                })
                .setNegativeButton(App.INSTANCE.getString(R.string.strBaseActivity211), (dialog, which) -> dialog.dismiss())
                .create();
        tipDialog.show();
    }

    protected void openActivity(Class<? extends Activity> clz, int requestCode) {
        if (MyAppLication.isLogin()) {
            startActivityForResult(new Intent(mActivity, clz), requestCode);
        } else {
            startActivity(new Intent(mActivity, LoginActivity.class));
        }
    }


    public void logout() {
        RxBus.getDefault().send(Constants.USER_LOGOUT);
        SPUtils.clear(MyAppLication.getMobile());
        MyAppLication.exitApp(MainActivity.class);
        LoginActivity.start(this);
    }


    public boolean checkEmpty(EditText editText) {
        return TextUtils.isEmpty(editText.getText());
    }




}
